`
https://leetcode.cn/problems/minimize-maximum-of-array/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var minimizeArrayValue = function (nums) {
  const n = nums.length

  // 操作后 nums 数组中的最大值能否为 lim
  const check = (lim) => {
    let extra = 0
    for (let i = n - 1; i >= 1; i--) {
      extra = Math.max(nums[i] + extra - lim, 0)
    }
    return nums[0] + extra <= lim
  }

  let left = -1, right = Math.max(...nums)
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};